package com.xiaoshi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshi.exception.PlatformException;
import com.xiaoshi.system.dto.SysRoleDto;
import com.xiaoshi.system.entity.SysRole;
import com.xiaoshi.system.entity.SysUserRole;
import com.xiaoshi.system.mapper.SysRoleMapper;
import com.xiaoshi.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshi.system.service.ISysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author yuankai
 * @since 2022-03-21
 */
@Service
@Transactional
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public boolean saveRole(SysRoleDto dto) {
        SysRole sysRole = new SysRole();
        SysRole byStr = getByStr(dto.getRoleCode(), null);
        if(byStr != null){
            throw new PlatformException("编码重复！");
        }
        SysRole byStr1 = getByStr(null, dto.getRoleName());
        if(byStr1 != null){
            throw new PlatformException("角色名称重复！");
        }
        BeanUtil.copyProperties(dto,sysRole);
        return this.save(sysRole);
    }

    @Override
    public boolean updateRole(SysRole dept) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ne(SysRole.ROLE_ID,dept.getRoleId());
        queryWrapper.eq(SysRole.DEL_TAG,0);
        queryWrapper.eq(SysRole.ROLE_NAME,dept.getRoleName());
        List<SysRole> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            throw new PlatformException("角色名称已存在！");
        }
        return  this.updateById(dept);
    }

    @Override
    public boolean delRoleById(Long roleId) {

        QueryWrapper<SysUserRole> qw = new QueryWrapper<>();
        qw.eq(SysUserRole.ROLE_ID,roleId);
        List<SysUserRole> list = sysUserRoleService.list(qw);
        if(!CollectionUtil.isEmpty(list)){
            throw new PlatformException("该角色还有 ["+list.size()+"] 人在使用！");
        }

        LambdaUpdateWrapper<SysRole> luw = new LambdaUpdateWrapper();
        luw.eq(SysRole::getRoleId,roleId).set(SysRole::getDelTag,1);
        return this.update(luw);
    }

    @Override
    public IPage getPage(Page page, String deptName) {
        QueryWrapper<SysRole> qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(deptName)){
            qw.eq(SysRole.ROLE_NAME,deptName);
        }
        return this.page(page,qw);
    }

    @Override
    public List<SysRole> getList() {
        QueryWrapper<SysRole> qw = new QueryWrapper();
        qw.eq(SysRole.DEL_TAG,0);
        return this.list(qw);
    }

    public SysRole getByStr(String deptCode,String deptName){
        QueryWrapper<SysRole> qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(deptCode)){
            qw.eq(SysRole.ROLE_CODE,deptCode);
        }
        if(StrUtil.isNotEmpty(deptName)){
            qw.eq(SysRole.ROLE_NAME,deptName);
        }
        qw.eq(SysRole.DEL_TAG,0);
        return this.getOne(qw);
    }
}
